package com.lanou.connotationjoke.tool;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.connotationjoke.db.DaoMaster;
import com.lanou.connotationjoke.db.DaoSession;
import com.lanou.connotationjoke.db.HomeCollectEntityDao;
import com.lanou.connotationjoke.db.HomeJokeEntityDao;
import com.lanou.connotationjoke.db.HomePictureEntityDao;
import com.lanou.connotationjoke.db.HomeRecommendationEntityDao;
import com.lanou.connotationjoke.db.HomeSubscriptionEntityDao;
import com.lanou.connotationjoke.db.HomeVideoEntityDao;


/**
 * Created by Risky on 15/10/29.
 */
public class DaoSingleton {
    private static final String DATABASE_NAME = "connotationjoke.db";

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private HomeJokeEntityDao homeJokeEntityDao;
    private HomeVideoEntityDao homeVideoEntityDao;
    private HomePictureEntityDao homePictureEntityDao;
    private HomeSubscriptionEntityDao homeSubscriptionEntityDao;
    private HomeRecommendationEntityDao homeRecommendationEntityDao;
    private HomeCollectEntityDao homeCollectEntityDao;

    private DaoSingleton() {
        context = MyAppllication.getContext();
    }

    public static DaoSingleton getInstance() {
        return SingletonHolder.instance;
    }
    private static final class SingletonHolder{
        private static final DaoSingleton instance = new DaoSingleton();
    }

    private DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    private SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    private DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    private DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public HomeJokeEntityDao getHomeJokeEntityDao(){
        if (homeJokeEntityDao == null){
            homeJokeEntityDao = getDaoSession().getHomeJokeEntityDao();
        }
        return homeJokeEntityDao;
    }
    public HomeVideoEntityDao getHomeVideoEntityDao(){
        if (homeVideoEntityDao == null){
            homeVideoEntityDao = getDaoSession().getHomeVideoEntityDao();
        }
        return homeVideoEntityDao;
    }

    public HomePictureEntityDao getHomePictureEntityDao(){
        if (homePictureEntityDao == null){
            homePictureEntityDao = getDaoSession().getHomePictureEntityDao();
        }
        return homePictureEntityDao;
    }
    public HomeSubscriptionEntityDao getHomeSubscriptionEntityDao(){
        if (homeSubscriptionEntityDao == null){
            homeSubscriptionEntityDao = getDaoSession().getHomeSubscriptionEntityDao();
        }
        return homeSubscriptionEntityDao;
    }
    public HomeRecommendationEntityDao getHomeRecommendationEntityDao(){
        if (homeRecommendationEntityDao == null){
            homeRecommendationEntityDao = getDaoSession().getHomeRecommendationEntityDao();
        }
        return homeRecommendationEntityDao;
    }
    public HomeCollectEntityDao getHomeCollectEntityDao(){
        if (homeCollectEntityDao == null){
            homeCollectEntityDao = getDaoSession().getHomeCollectEntityDao();
        }
        return homeCollectEntityDao;
    }
}
